package com.sirdc.modules.sys.web.threeSevenAndInterView;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.utils.ObjectUtils;
import net.huashitong.interview.entity.EnterpriseRegister;
import net.huashitong.interview.filter.EnterpriseRegisterFilter;
import net.huashitong.interview.service.EnterpriseRegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("web/EnterpriseRegister")
public class WebEnterpriseRegisterController extends JsonBaseController {

    @Autowired
    private EnterpriseRegisterService service;
    @Override
    protected String getView(String s) {
        return null;
    }

    /**
     * 服务企业登记流转表
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getindex",method = RequestMethod.POST)
    public Message getIndex(@RequestBody EnterpriseRegisterFilter filter){
        String fillDate = "";
        if(ObjectUtils.isBlank(filter.getFillDate())){
            //获取数据库中最新的时间
            fillDate = service.getNewFillDate();
            if("0000".equals(fillDate)){
                return coverMessage("200", "暂无数据");
            }
            filter.setFillDate(fillDate.substring(0,6));
        }else{
            fillDate = filter.getFillDate();
        }
        List<EnterpriseRegister> list = service.queryRegister(filter);
        if(list.size()==0){
            return coverMessage("200", "暂无数据");
        }
        List<Map<String, Object>> result = service.baseResult(list);
        Map<String, Object> map2 = new HashMap<>();
        map2.put("fillDate",fillDate);
        map2.put("year",fillDate.substring(0,4));
        map2.put("month",fillDate.substring(4,6));
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("list",result);
        resultMap.put("time",map2);
        return coverMessage("200", "获取成功", resultMap);
    }

    /**
     * 服务企业登记流转表 详情页
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getdetailbyid",method = RequestMethod.POST)
    public Message getDetailById(@RequestBody EnterpriseRegisterFilter filter){
        EnterpriseRegister enterpriseRegister = service.getById(filter.getSysId());
        if(ObjectUtils.isBlank(enterpriseRegister)){
            return coverMessage("200", "无详细信息");
        }
        Map<String,String> map = new HashMap<>();

        map.put("sysId",filter.getSysId());
        map.put("leaderName",ObjectUtils.isNotBlank(enterpriseRegister.getLeaderName())?enterpriseRegister.getLeaderName():"-");
        map.put("fillDate",ObjectUtils.isNotBlank(enterpriseRegister.getFillDate())?enterpriseRegister.getFillDate():"-");
        map.put("year",ObjectUtils.isNotBlank(enterpriseRegister.getFillDate())?enterpriseRegister.getFillDate().substring(0,4):"-");
        map.put("month",ObjectUtils.isNotBlank(enterpriseRegister.getFillDate())?enterpriseRegister.getFillDate().substring(4,6):"-");
        map.put("fillUnitId",ObjectUtils.isNotBlank(enterpriseRegister.getFillUnitId())?enterpriseRegister.getFillUnitId():"-");
        map.put("interviewEnterprise",ObjectUtils.isNotBlank(enterpriseRegister.getInterviewEnterprise())?enterpriseRegister.getInterviewEnterprise():"-");
        map.put("interviewDate",ObjectUtils.isNotBlank(enterpriseRegister.getInterviewDate())?enterpriseRegister.getInterviewDate():"-");
        map.put("situation",ObjectUtils.isNotBlank(enterpriseRegister.getSituation())?enterpriseRegister.getSituation():"-");
        map.put("localSolve",ObjectUtils.isNotBlank(enterpriseRegister.getLocalSolve())?enterpriseRegister.getLocalSolve():"-");
        map.put("officeOpinion",ObjectUtils.isNotBlank(enterpriseRegister.getOfficeOpinion())?enterpriseRegister.getOfficeOpinion():"-");
        map.put("governmentOpinion",ObjectUtils.isNotBlank(enterpriseRegister.getGovernmentOpinion())?enterpriseRegister.getGovernmentOpinion():"-");
        map.put("feedback",ObjectUtils.isNotBlank(enterpriseRegister.getFeedback())?enterpriseRegister.getFeedback():"-");

        return coverMessage("200", "获取成功", map);
    }

    /**
     * 服务企业登记流转表  添加功能
     * @param enterpriseRegister
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addenterpriseregister",method = RequestMethod.POST)
    public Message addEnterpriseRegister(@RequestBody EnterpriseRegister enterpriseRegister){
        service.save(enterpriseRegister);
        return coverMessage("200", "添加成功");
    }

    /**
     * 服务企业登记流转表  修改功能
     * @param enterpriseRegister
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/editenterpriseregister",method = RequestMethod.POST)
    public Message editEnterpriseRegister(@RequestBody EnterpriseRegister enterpriseRegister){
        EnterpriseRegister newBean = service.getById(enterpriseRegister.getSysId());
        if(ObjectUtils.isNotBlank(enterpriseRegister.getLeaderName())){
            newBean.setLeaderName(enterpriseRegister.getLeaderName());
        }
        if(ObjectUtils.isNotBlank(enterpriseRegister.getFillDate())){
            newBean.setFillDate(enterpriseRegister.getFillDate());
        }
        if(ObjectUtils.isNotBlank(enterpriseRegister.getFillUnitId())){
            newBean.setFillUnitId(enterpriseRegister.getFillUnitId());
        }
        if(ObjectUtils.isNotBlank(enterpriseRegister.getInterviewEnterprise())){
            newBean.setInterviewEnterprise(enterpriseRegister.getInterviewEnterprise());
        }
        if(ObjectUtils.isNotBlank(enterpriseRegister.getInterviewDate())){
            newBean.setInterviewDate(enterpriseRegister.getInterviewDate());
        }
        if(ObjectUtils.isNotBlank(enterpriseRegister.getSituation())){
            newBean.setSituation(enterpriseRegister.getSituation());
        }
        if(ObjectUtils.isNotBlank(enterpriseRegister.getLocalSolve())){
            newBean.setLocalSolve(enterpriseRegister.getLocalSolve());
        }
        if(ObjectUtils.isNotBlank(enterpriseRegister.getOfficeOpinion())){
            newBean.setOfficeOpinion(enterpriseRegister.getOfficeOpinion());
        }
        if(ObjectUtils.isNotBlank(enterpriseRegister.getGovernmentOpinion())){
            newBean.setGovernmentOpinion(enterpriseRegister.getGovernmentOpinion());
        }
        if(ObjectUtils.isNotBlank(enterpriseRegister.getFeedback())){
            newBean.setFeedback(enterpriseRegister.getFeedback());
        }
        service.update(newBean);
        return coverMessage("200", "修改成功");
    }

    /**
     * 服务企业登记流转表  删除功能
     * @param enterpriseRegister
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteenterpriseregister",method = RequestMethod.POST)
    public Message deleteEnterpriseRegister(@RequestBody EnterpriseRegister enterpriseRegister){
        service.deleteById(enterpriseRegister.getSysId());
        return coverMessage("200", "删除成功");
    }

}
